package com.changanhua.io.file;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author changanhua
 */
public class FileDemo {

    private static final String FILE_PATH = "/home/changanhua/Desktop/a.txt";

    private static final String STRING_01 = "12\r\n";
    private static final String STRING_02 = "123456789\r\n";

    public static void main(String[] args) {
        File file = new File(FILE_PATH);
        System.out.println("STRING_01.length : " + STRING_01.getBytes(StandardCharsets.UTF_8).length);
        normalByteWriterTest(file);
        // normalWriterTest(file);
    }


    static void normalByteWriterTest(File file) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 5);
            for (int i = 0; i < 2; i++) {
                // 此处需要在字符串转换为字节数组时，手动进行转换为字节数组，调用java.lang.String.getBytes(java.nio.charset.Charset)
                // 此处对于buf[] 当到达临近8192数组的时，进行flushbuffer
                if (i == 1){
                    bufferedOutputStream.write(STRING_02.getBytes(StandardCharsets.UTF_8));
                    break;
                }
                bufferedOutputStream.write(STRING_01.getBytes(StandardCharsets.UTF_8));
            }
            // bufferedOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void bufferWriterTest(File file) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            // 按照指定字符编码格式，字符编码为字节存储
            OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
            // 使用IO流中的Buffer缓冲流，8KB（8192byte）发送一次数据
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            for (int i = 0; i < 1000; i++) {
                bufferedWriter.write(STRING_01);
            }
            // bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void normalWriterTest(File file) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            // 按照指定字符编码格式，在writer期间进行指定，字符编码为字节存储
            OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
            for (int i = 0; i < 10; i++) {
                writer.write(STRING_01);
            }
            // writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}

/*
 * 乱码演示
 * 为什么buffer快
 *   write演示用户态和内核态切换次数
 * bufferReader按行读取
 * buffer数据丢失演示和原因分析
 * 普通IO和flushIO区别
 * 包装流先后关闭问题
 *
 * */

/*        try {
            // 构建File抽象对象
            FileInputStream fileInputStream = new FileInputStream(file);
            // 按照UTF-8字符编码格式解码
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            // InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.US_ASCII);
            // 封装BufferReader缓冲区
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            // 按照行数读取
            while (bufferedReader.readLine() != null) {
                System.out.println(bufferedReader.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/